<?php

/**
 * This is the model class for table "mtsys_base".
 *
 * The followings are the available columns in table 'mtsys_base':
 * @property integer $BASE_ID
 * @property integer $PROVINCE_ID
 * @property integer $CITY_ID
 * @property integer $COUNTY_ID
 * @property integer $LEVEL_N
 * @property string $BASE_NAME
 */
class MtsysBase extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return MtsysBase the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'mtsys_base';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('PROVINCE_ID, CITY_ID, COUNTY_ID, BASE_NAME, BASE_TYPE', 'required'),
			array('PROVINCE_ID, CITY_ID, COUNTY_ID, LEVEL_N', 'numerical', 'integerOnly'=>true),
			array('BASE_NAME', 'length', 'max'=>50),
			array('BASE_NAME', 'unique'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('BASE_ID, PROVINCE_ID, CITY_ID, COUNTY_ID, LEVEL_N, BASE_NAME, BASE_TYPE', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'PROVINCE'=>array(self::BELONGS_TO, 'MtsysProvince', 'PROVINCE_ID'),
			'CITY'=>array(self::BELONGS_TO, 'MtsysCity', 'CITY_ID'),
			'COUNTY'=>array(self::BELONGS_TO, 'MtsysCounty', 'COUNTY_ID'),
			'TYPE'=>array(self::BELONGS_TO,'Btstype', 'BASE_TYPE'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'BASE_ID' => '站点ID',
			'PROVINCE_ID' => '所在省',
			'CITY_ID' => '所在市',
			'COUNTY_ID' => '所在区/县',
			'LEVEL_N' => '站点级别',
			'BASE_TYPE' => '站点类型',
			'BASE_NAME' => '站点名称',
		);
	}

	
	//added by hyq 2012-12-20
	public function searchByUserid()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
		$bases;
		$sqlbase;
		//get batterys from userid
		$mUser=User::model()->findByPk(Yii::app()->user->id);
		$unit_level = $mUser->unit_level;
		$unit_id = $mUser->unit_id;
			
		//		echo "level=".$unit_level;
		//get right by uid
		switch ($mUser->unit_level){
			case 4:
				$sqlbase = "select * from mtsys_base where BASE_ID in ( ".$unit_id." )";
	
				break;
			case 3:
				$sqlbase = "select * from mtsys_base where COUNTY_ID = ".$unit_id;
	
				break;
			case 2:
				$sqlbase = "select * from mtsys_base where CITY_ID = ".$unit_id;
	
				break;
			case 1:
				$sqlbase = "select * from mtsys_base where PROVINCE_ID = ".$unit_id;
	
				break;
			case 0:
				$sqlbase = "select * from mtsys_base " ;
					
					
				break;
			default:
				break;
		}
			
		$bases = new CSqlDataProvider($sqlbase, array());	
		return $bases;
	}
	
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	/*	
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		var_dump($this->BASE_ID);
		$criteria->compare('BASE_ID',$this->BASE_ID);
		
		$criteria->compare('PROVINCE_ID',$this->PROVINCE_ID);
		$criteria->compare('CITY_ID',$this->CITY_ID);
		$criteria->compare('COUNTY_ID',$this->COUNTY_ID);
		$criteria->compare('LEVEL_N',$this->LEVEL_N);
		$criteria->compare('BASE_NAME',$this->BASE_NAME,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	*/
	public function search()
	{
		
		$isAdmin = Yii::app()->getModule('user')->isAdmin();
		$user = User::model()->findByPk(Yii::app()->user->id);
		$unit_ids = array();
		$user_ids = array();
		$city_ids = array();
		$county_ids = array();
		$allbases = array();
		if(!$isAdmin){
			switch($user->unit_level){
				case 1:

						
					$citys = MtsysCity::model()->findAll('PROVINCE_ID=:pid',array(':pid'=>$user->unit_id));
					foreach($citys as $city){
						$condition = 'CITY_ID = :city_id';
						$params = array(':city_id' => $city->CITY_ID);
						$bases = MtsysBase::model()->findAll($condition, $params);
						foreach ($bases as $_base){
							if(array_key_exists($_base->BASE_ID, $allbases) == FALSE)
								array_push($allbases, $_base->BASE_ID);
						}
					}					
						
					$countys = MtsysCounty::model()->findAll('CITY_ID in (:cids)', array(':cids'=>implode(',',$city_ids)));
					foreach($countys as $county){
						$condition = 'COUNTY_ID = :city_id';
						$params = array(':city_id' => $county->COUNTY_ID);
						$bases = MtsysBase::model()->findAll($condition, $params);
						foreach ($bases as $_base){
							if(array_key_exists($_base->BASE_ID, $allbases) == FALSE)
								array_push($allbases, $_base->BASE_ID);
						}
					}
						
					$bases = MtsysBase::model()->findAll('PROVINCE_ID=:pid',array(':pid'=>$user->unit_id));
					foreach($bases as $base){
						if(array_key_exists($_base->BASE_ID, $allbases) == FALSE)
							array_push($allbases, $_base->BASE_ID);
					}
						
					break;
				case 2:
	
					$countys = MtsysCounty::model()->findAll('CITY_ID=:cid',array(':cid'=>$user->unit_id));
					foreach($countys as $county){
					$condition = 'COUNTY_ID = :city_id';
						$params = array(':city_id' => $county->COUNTY_ID);
						$bases = MtsysBase::model()->findAll($condition, $params);
						foreach ($bases as $_base){
							if(array_key_exists($_base->BASE_ID, $allbases) == FALSE)
								array_push($allbases, $_base->BASE_ID);
						}
					}
						
					$bases = MtsysBase::model()->findAll('CITY_ID=:pid',array(':pid'=>$user->unit_id));
					foreach($bases as $base){
						if(array_key_exists($_base->BASE_ID, $allbases) == FALSE)
							array_push($allbases, $_base->BASE_ID);
					}
					break;
				case 3:											
					$bases = MtsysBase::model()->findAll('COUNTY_ID=:pid',array(':pid'=>$user->unit_id));
					foreach($bases as $_base){
						if(array_key_exists($_base->BASE_ID, $allbases) == FALSE)
							array_push($allbases, $_base->BASE_ID);
					}
					
					break;
				case 4:
					$bases = User::model()->getBases($user->id);
					foreach($bases as $_base){
						if(array_key_exists($_base->BASE_ID, $allbases) == FALSE)
							array_push($allbases, $_base->BASE_ID);
					}
				
					break;
			}
		}else{
			$bases = MtsysBase::model()->findAll();
		//	var_dump($bases);
			foreach ($bases as $_base){
				if(array_key_exists($_base->BASE_ID, $allbases) == FALSE)
					array_push($allbases, $_base->BASE_ID);
			}
		}
	
		$criteria=new CDbCriteria;
		//var_dump($allbases);
		
		if($this->BASE_NAME){
			$base = MtsysBase::model()->find('BASE_NAME=:name',array(':name'=>$this->BASE_NAME));
			if($base){
				$exists = 0;
				foreach($allbases as $_base){
					if($base->BASE_ID === $_base){
						$exists = 1;
						break;
					}
				}
				if($exists){					
					$criteria->compare('BASE_NAME',$this->BASE_NAME);
				}else{
					$criteria->compare('BASE_NAME','Not EXsitS');					
				}
			}else{
				$criteria->compare('BASE_NAME','Not EXsitS');
			}			
		}
		else{
			if($isAdmin)
				$criteria->condition = '';
			else if(empty($allbases)){
				$criteria->compare('BASE_NAME','Not EXsitS');
			}else{
				$criteria->condition = ($isAdmin)?'':'BASE_ID in ('.implode(',',$allbases).')' ;
			}
				
		}
			
			//$criteria->condition = ($isAdmin)?'':'BASE_ID in ('.implode(',',$allbases).')' ;	
		
		$dataProvider=new CActiveDataProvider($this, array(				
				'criteria'=>$criteria,
				
		));
	
		return $dataProvider;
	}

	
	public function getUser($id)
	{
		$user = User::model()->find('unit_id=:uid and unit_level=:ul',array(':uid'=>$id,':ul'=>4));
	
		//var_dump($province_id);
		if($user){
		if(!empty($user->surname) && !empty($user->telephone)){
				return $user->surname . '-' . $user->telephone;
			}else if($user->surname == NULL && !empty($user->telephone)){				
				return $user->username . '-' . $user->telephone;
			}else if($user->surname == NULL && empty($user->telephone) ){
				return  $user->username . '-' . '暂无电话';		
			}else if(!empty($user->surname) && $user->telephone == NULL){
				return $user->surname . '-' . '暂无电话';						
			}else{
				return '暂无';
			}
		}else{
			$users = User::model()->findAll('unit_level=:ul',array(':ul'=>4));
			foreach($users as $user){
				if(strripos($user->unit_id,',')){
					$uid = $user->unit_id;
						
					$uid = strtok($uid,',');
					while ($uid !== false) {
						if(intval($uid) == $id ){
							if(!empty($user->surname) && !empty($user->telephone)){
								return $user->surname . '-' . $user->telephone;
							}else if($user->surname == NULL && !empty($user->telephone)){				
								return $user->username . '-' . $user->telephone;
							}else if($user->surname == NULL && empty($user->telephone) ){
								return  $user->username . '-' . '暂无电话';		
							}else if(!empty($user->surname) && $user->telephone == NULL){
								return $user->surname . '-' . '暂无电话';						
							}else{
								return '暂无';
							}						
						}							
						$uid = strtok(',');
					}
				}
			}
		}
		return '暂无';
	}
	
	public function getUserIds($id)
	{
		$allusers = array();
		$users = User::model()->findAll('unit_id=:uid and unit_level=:ul',array(':uid'=>$id,':ul'=>4));
	
		//var_dump($province_id);
		if($users){
			$allusers = $users;
			//return $allusers;
		}
		
		//append users manage multipy bases
		{
			$users = User::model()->findAll('unit_level=:ul',array(':ul'=>4));
			foreach($users as $user){
				if(strripos($user->unit_id,',')){
					$uid = $user->unit_id;
	
					$uid = strtok($uid,',');
					while ($uid !== false) {
						if(intval($uid) == $id ){
							array_push($allusers , $user);
						}
						$uid = strtok(',');
					}
				}
			}
		}
		
		return $allusers;
	}
}